﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;



namespace DataAccess
{
    public class DatabaseDao
    {
        //public string connectionStr = @"Data Source=MOJO-PC\SQLEXPRESS;Initial Catalog=fd;User ID=sa;Password=12345";
        SqlConnection connection = new SqlConnection();
        
                                         
        
        public DatabaseDao()
        {
            //connection = new SqlConnection(@"Data Source=MONICA-PC\SQLEXPRESS;Initial Catalog=fd;User ID=sa1;Password=password");
            connection = new SqlConnection(@"Data Source=localhost;Initial Catalog=fd;User ID=sa1;Password=password");
        }

        public int ExecuteNonQuery(SqlParameter[] parameters, string sp)
        {
            int returnValue = -1;
            SqlCommand command = new SqlCommand();
            command.Connection = connection;

            command.CommandText = sp;

            command.CommandType = CommandType.StoredProcedure;

            if (parameters != null)
            {
                foreach (SqlParameter param in parameters)
                {
                    command.Parameters.Add(param);
                }
            }

            try
            {
                this.connection.Open();

                command.Prepare();

                returnValue = command.ExecuteNonQuery();

                this.connection.Close();

                // this.connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return returnValue;
        }

        public int ExecuteScalar(SqlParameter[] parameters, string sp)
        {
            int returnValue = -1;
            SqlCommand command = new SqlCommand();
            command.Connection = connection;

            command.CommandText = sp;

            command.CommandType = CommandType.StoredProcedure;

            if (parameters != null)
            {
                foreach (SqlParameter param in parameters)
                {
                    command.Parameters.Add(param);
                }
            }

            try
            {
                this.connection.Open();

                command.Prepare();

                returnValue = (Int32)command.ExecuteScalar();

                this.connection.Close();

                // this.connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return returnValue;
        }
        
        public DataTable ExecuteReturnQuery(SqlParameter [] parameters, string sp)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            DataTable dt = new DataTable();

            command.CommandText = sp;

            command.CommandType = CommandType.StoredProcedure;

            if (parameters != null)
            {
                foreach (SqlParameter param in parameters)
                {
                    command.Parameters.Add(param);
                }
            }

            try
            {
                this.connection.Open();

                command.Prepare();

                SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
                
                dt.Load(dr);

               // this.connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return dt;
        }
    }
}
